#ifndef XIP_H
#define XIP_H
#include "protocol.h"
#include "xnet_err.h"
#include "xnet_tiny.h"
#include "xethernet.h"

#define XNET_VERSION_IPV4 4    // ipv4版本号
#define XNET_IP_DEFAULT_TTL 64 // ip默认的存活时间

// 16位校验和检验算法
uint16_t checksum16(uint16_t *data, uint16_t len, uint16_t pre_sum, int complement);
// udp tcp伪校验和校验算法
uint16_t checksum_peso(const xipaddr_t *src_ip, const xipaddr_t *dest_ip,
                       uint8_t protocol, uint16_t *buf, uint16_t len);
#pragma(1)
typedef struct _xip_hdr_t
{
    uint8_t hdr_len : 4; // 4个字节
    uint8_t version : 4; // 4个字节
    uint8_t type_of_service;
    uint16_t total_len;
    uint16_t identification;
    uint16_t flags_fragmentoffset;
    uint8_t time_to_live;
    uint8_t protocol;
    uint16_t header_checksum;
    uint8_t src_ip[XNET_IPV4_ADDR_SIZE];
    uint8_t dest_ip[XNET_IPV4_ADDR_SIZE];
} xip_hdr_t;
#pragma()

void xip_init(void);
void xip_in(xnet_packet_t *packet);
xnet_err_t xip_out(xnet_protocol_t protocal, xipaddr_t *dest_ip, xnet_packet_t *packet);

#endif
